<?php
/**
* @filename course_note.php 2016-9-6 02:10:56x
* @author Yunfeng Zhang <306232452@qq.com>
* @version 1.0.0
* @copyright DZF (c) 2016, Yunfeng Zhang
*/
header("Content-Type: text/html;charset=utf-8");
class ctrl_course_note extends course_note {
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
	
	static function &instance() {
		static $object;
		if (empty($object)) {
			$object = new self();
		}
		return $object;
	}
	public function do_detail() {		
		global $_G;	
		$page = max(1, intval($_GET['page']));
		$perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : '1000';;
		$start=(($page-1) * $perpage);
		$wheresql = "";		
		$wheresql .= " AND cn.isdelete = 0 AND cn.user_id = '".$_G['user_id']."' ";
		//课时id
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';;
		if($ch_id){
			$wheresql .= " AND cn.ch_id = ".$ch_id;
		}
		$page_info = isset($_REQUEST['page_info']) && !empty($_REQUEST['page_info']) ? $_REQUEST['page_info'] : '';
		$orderby = " ORDER BY cn.cn_id DESC ";
		//DEBUG 初始化请求请求获取一页列表数据的参数
		$page_condition=array(
				'page' => $page,//int 请求页面 页码
				'limit' => $limit,//int 每页请求个数
				'perpage' => $perpage,//int 每页显示个数
				'wheresql' => $wheresql,//string //条件SQL语句
				'orderby' => $orderby,//string 排序规则
		);		
		$page_result = $this->index('course_note',$page_condition);
			
		//$content_cate_title_array = $this->get_field_value_by_id('content_cate','info_cateid',$info_cateid_array,'title');
		//DEBUG 获取各类信息分类名称 结束		
		//DEBUG 返回输出
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';		
		if($page_result){		
			$return['code'] = '0';
			if($page_info){
				$return['data']=$page_result;
			}else{
				$return['data']=$page_result['page_data'];
			}
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}		 
			
	}
	
	/*
	 *  添加笔记
	 */
	public function do_add() {
		global $_G;	
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$course_note = isset($_REQUEST['course_note']) && !empty($_REQUEST['course_note']) ? $_REQUEST['course_note']:'';		
				
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		foreach($course_note AS $key => $value){
			if(is_array($value) && !empty($value)){
				$course_note[$key] = implode(",", $value);
			}
		}
		$course_note['create_dateline'] = TIMESTAMP;
		$course_note['modify_dateline'] = TIMESTAMP;
		$course_note['user_id'] = $_G['user_id'];
		$insert_data = $this->add('course_note',$course_note);	
		
		if($insert_data){
										
			$return['code'] = '0';
			$return['data']=$insert_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
		
	}
	
	/*
	 *  编辑笔记
	 */
	public function do_edit() {
		global $_G;
		//$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$course_note = isset($_REQUEST['course_note']) && !empty($_REQUEST['course_note']) ? $_REQUEST['course_note']:'';		
		$cn_id = isset($_REQUEST['cn_id']) ? $_REQUEST['cn_id'] : '';
		if($cn_id){			
			$course_note['modify_dateline'] = TIMESTAMP;
			//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
			foreach($course_note AS $key => $value){
				if(is_array($value) && !empty($value)){
					$course_note[$key] = implode(",", $value);
				}
			}
			$where = array('cn_id'=>$cn_id);
			$effect_row = $this->edit('course_note',$course_note,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$course_note;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}			
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
	
	/*
	 *  删除笔记
	 */
	public function do_delete() {
		global $_G;
		$cn_id = isset($_REQUEST['cn_id']) ? $_REQUEST['cn_id'] : '';
		if($cn_id){
			$effect_row = $this->delete("course_note", array("cn_id" => $cn_id), $limit=1);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}
	}
}
?>